﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using WucCloud.Application;
using WucCloud.Entity;
using WucCloud.Utility;

namespace WucCloud.WebApi.Areas.SystemManage.Controllers
{
    [Route("api/[controller]/[action]")]
    [Area("SystemManage")]
    [Authorize(Policy = "Others")]
    public class MenuInfoController : ControllerBase
    {
        /// <summary>
        /// 菜单信息服务类
        /// </summary>
        private readonly MenuInfoService _service;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="service"></param>
        public MenuInfoController(MenuInfoService service)
        {
            _service = service;
        }

        #region 获取数据
        /// <summary>
        /// 通过Id查询
        /// </summary>
        /// <param name="keyId">主键Id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<UnifyResult>> FindById([Required] string keyId)
        {
            try
            {
                //通过Id查询用户数据
                var entity = await _service.FindById(keyId);
                //判断是否获取到数据
                if (entity == null)
                {
                    return new UnifyResult<dynamic>
                    {
                        StatusCode = ResultCode.NoneData,
                        Message = ResultCode.NoneData.Description(),
                        Data = null,
                        TotalCount = 0
                    };
                }
                //返回获取到的数据
                else
                {
                    return new UnifyResult<dynamic>
                    {
                        StatusCode = ResultCode.Success,
                        Message = ResultCode.Success.Description(),
                        Data = entity,
                        TotalCount = 1
                    };
                }
            }
            catch (Exception ex)
            {
                return new UnifyResult()
                {
                    StatusCode = ResultCode.Error,
                    Message = ex.Message.ToString()
                };
            }
        }

        /// <summary>
        /// 获取树形菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<UnifyResult>> GetMenuTree(string keyvalue)
        {
            try
            {
                //获取到所有菜单信息
                var data = await _service.GetList(keyvalue);

                List<TreeNode> treeNodes = new List<TreeNode>();
                foreach (MenuInfoEntity item in data)
                {
                    TreeNode tree = new TreeNode();
                    tree.id = item.KeyId;
                    tree.pid = item.ParentId;
                    tree.title = item.MenuName;
                    tree.icon = item.MenuIcon;
                    tree.href = item.MenuUrl;

                    treeNodes.Add(tree);
                }

                if (treeNodes.Count == 0)
                {
                    return new UnifyResult<dynamic>
                    {
                        StatusCode = ResultCode.NoneData,
                        Message = ResultCode.NoneData.Description(),
                        Data = null,
                        TotalCount = 0
                    };
                }
                else
                {
                    return new UnifyResult<dynamic>
                    {
                        StatusCode = ResultCode.Success,
                        Message = ResultCode.Success.Description(),
                        Data = TreeNodeExtensions.TreeList(treeNodes),
                        TotalCount = treeNodes.Count
                    };
                }
            }
            catch (Exception ex)
            {
                return new UnifyResult()
                {
                    StatusCode = ResultCode.Error,
                    Message = ex.Message.ToString()
                };
            }
        }

        /// <summary>
        /// 自定义条件分页查询
        /// </summary>
        /// <param name="keyvalue">查询条件（编码/姓名）</param>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageSize">行数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<UnifyResult>> QueryPagedList(string keyvalue, [Required] int pageIndex, [Required] int pageSize)
        {
            //分页组件类
            PageModel page = new PageModel
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
            };

            try
            {
                //获取未锁定账户的用户分页数据
                var entity = await _service.QueryPagedList(keyvalue, page);

                //判断是否获取到数据
                if (entity.Count == 0)
                {
                    return new UnifyResult<dynamic>
                    {
                        StatusCode = ResultCode.NoneData,
                        Message = ResultCode.NoneData.Description(),
                        Data = null,
                        TotalCount = 0
                    };
                }
                //返回获取到的数据
                else
                {
                    return new UnifyResult<dynamic>
                    {
                        StatusCode = ResultCode.Success,
                        Message = ResultCode.Success.Description(),
                        Data = entity,
                        TotalCount = entity.Count
                    };
                }
            }
            catch (Exception ex)
            {
                return new UnifyResult()
                {
                    StatusCode = ResultCode.Error,
                    Message = ex.Message.ToString()
                };
            }
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 保存数据（新增/编辑）
        /// </summary>
        /// <param name="keyId">主键Id</param>
        /// <param name="entity">实体信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<UnifyResult>> SaveChange(string keyId, [FromForm] MenuInfoEntity entity)
        {
            try
            {
                bool result = await _service.SaveChange(keyId, entity);
                if (!result)
                {
                    return new UnifyResult()
                    {
                        StatusCode = ResultCode.Failed,
                        Message = ResultCode.Failed.Description()
                    };
                }
                //操作成功
                else
                {
                    return new UnifyResult()
                    {
                        StatusCode = ResultCode.Success,
                        Message = ResultCode.Success.Description()
                    };
                }
            }
            catch (Exception ex)
            {
                return new UnifyResult()
                {
                    StatusCode = ResultCode.Error,
                    Message = ex.Message.ToString()
                };
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyId">主键Id</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ActionResult<UnifyResult>> Deleteable([Required] string keyId)
        {
            try
            {
                bool result = await _service.Deleteable(keyId);
                //操作失败
                if (!result)
                {
                    return new UnifyResult()
                    {
                        StatusCode = ResultCode.Failed,
                        Message = ResultCode.Failed.Description()
                    };
                }
                //操作成功
                else
                {
                    return new UnifyResult()
                    {
                        StatusCode = ResultCode.Success,
                        Message = ResultCode.Success.Description()
                    };
                }
            }
            catch (Exception ex)
            {
                return new UnifyResult()
                {
                    StatusCode = ResultCode.Error,
                    Message = ex.Message.ToString()
                };
            }
        }
        #endregion

    }
}
